/*
 * VerDetalleFactura.java
 *
 * Created on 26 de marzo de 2007, 19:33
 */

package vista;

import controlador.DetalleFactura;
import controlador.DetalleRemito;
import controlador.Factura;
import java.awt.Color;
import java.util.LinkedList;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author  PC
 */
public class VerDetalleFactura extends javax.swing.JFrame {
    
    private Factura factura;
    /** Creates new form VerDetalleFactura */
    public VerDetalleFactura(Factura factura) {
        this.factura = factura;
        initTabla();
        initComponents();
        
        tipo.setText(factura.getTipo());
        numero.setText(factura.getNumero());
        fecha.setText(factura.getFecha());
        comision2.setText("( " + factura.getComision() + "% )");
        comision.setText(factura.getComision() + "% ");
        setTotales();
        if(factura.isPagado() == false){
            if(factura.isCompleto()){
                completo.setText("Esta factura ha sido pagado en su TOTALIDAD");
                completo.setForeground(new Color(0,0,255));
            }
            else{
                completo.setText("Todavia quedan libros adeudados en referencia a este remito.");
                completo.setForeground(new Color(255,0,0));
            }
        }
        else{
            completo.setText("");
        }
        this.setTitle("Factura " + factura.getTipo() + factura.getNumero() + " de " + factura.getCliente().getNombre());
        
    }
    public DefaultTableModel getModel(){
        return this.dtm;
    }
    public void setTotales(){
        this.bruto.setText("$ " +  String.valueOf(factura.getTotal()));
        float desc = (factura.getTotal() * factura.getComision()) / 100;
        this.descuento.setText("$ " + String.valueOf(desc) );
        this.neto.setText("$ " + String.valueOf(factura.getTotal() - desc));
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        bruto = new javax.swing.JLabel();
        descuento = new javax.swing.JLabel();
        neto = new javax.swing.JLabel();
        comision2 = new javax.swing.JLabel();
        tipo = new javax.swing.JLabel();
        numero = new javax.swing.JLabel();
        fecha = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        comision = new javax.swing.JLabel();
        completo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        jLabel1.setText("Tipo:");

        jLabel2.setText("Numero:");

        jLabel3.setText("Fecha:");

        jTable1.setModel(getModel());
        jScrollPane1.setViewportView(jTable1);

        jButton1.setText("Cerrar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel4.setText("Total Bruto:");

        jLabel5.setText("Comision: ");

        jLabel6.setText("Total Neto:");

        bruto.setText("jLabel7");

        descuento.setText("jLabel8");

        neto.setText("jLabel9");

        comision2.setText("jLabel10");

        tipo.setText("jLabel7");

        numero.setText("jLabel7");

        fecha.setText("jLabel7");

        jLabel7.setText("Comision: ");

        comision.setText("jLabel8");

        completo.setText("jLabel8");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel7))
                        .addGap(15, 15, 15)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(comision)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(fecha, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(numero, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(tipo, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(completo, javax.swing.GroupLayout.PREFERRED_SIZE, 311, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 473, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(comision2))
                            .addComponent(jLabel6))
                        .addGap(24, 24, 24)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(neto, javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE)
                            .addComponent(descuento, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(bruto))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tipo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(numero))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(fecha))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(comision))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(completo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(comision2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel6))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(bruto)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(descuento)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(neto)))
                .addGap(17, 17, 17)
                .addComponent(jButton1)
                .addContainerGap(68, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed
    
    public void initTabla(){
        dtm = new DefaultTableModel();
        if(factura.isPagado()){
            dtm.addColumn("Remito");
            dtm.addColumn("Cod");
            dtm.addColumn("Titulo");
            dtm.addColumn("Precio");
            dtm.addColumn("Cant.");
            dtm.addColumn("Subtotal");
        }
        else{
            dtm.addColumn("Cod");
            dtm.addColumn("Titulo");
            dtm.addColumn("Precio");
            dtm.addColumn("Cant.");
            dtm.addColumn("Subtotal");
        }
        LinkedList detalles = factura.getDetalles();
        for(int i = 0; i < detalles.size(); i++){
            DetalleFactura df = (DetalleFactura) detalles.get(i);
            dtm.addRow(getFila(df));
        }
        detalles.clear();        
    }
    
    private Object[] getFila(DetalleFactura def){
        Object[] datos;
        if(def.getIdDetalleRemito() != 0){
            datos = new Object[6];
            datos[0] = new DetalleRemito(def.getIdDetalleRemito()).getRemito().getNumero();
            datos[1] = def.getEjemplar().getLibro().getId();
            datos[2] = def.getEjemplar().getLibro().getTitulo();
            datos[3] = def.getEjemplar().getPrecioUnitario();
            datos[4] = def.getCantidad(); //es la cantidad elegida
            datos[5] = (def.getEjemplar().getPrecioUnitario() * def.getCantidad());
        }
        else{
            datos = new Object[5];
            datos[0] = def.getEjemplar().getLibro().getId();
            datos[1] = def.getEjemplar().getLibro().getTitulo();
            datos[2] = def.getEjemplar().getPrecioUnitario();
            datos[3] = def.getCantidad();
            datos[4] = (def.getEjemplar().getPrecioUnitario() * def.getCantidad());            
        }
        return datos;
    }
    public void insertarFila(DetalleFactura def){
        this.dtm.addRow(getFila(def));
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel bruto;
    private javax.swing.JLabel comision;
    private javax.swing.JLabel comision2;
    private javax.swing.JLabel completo;
    private javax.swing.JLabel descuento;
    private javax.swing.JLabel fecha;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel neto;
    private javax.swing.JLabel numero;
    private javax.swing.JLabel tipo;
    // End of variables declaration//GEN-END:variables
    private DefaultTableModel dtm;
}
